package lanou3g.com.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import java.util.List;

import lanou3g.com.DataBean.DataBean;
import lanou3g.com.Fragment.RecommendFragment;
import lanou3g.com.NetUtil;
import lanou3g.com.smart_image_view.SmartImageView;

public class Delicious_food_Activity extends Activity implements View.OnClickListener {
    private final String LOG_TAG = "RInfoActivity";

    private SmartImageView shopImageView;

    /**
     * 返回按钮
     */
    private TextView backTextView;

    /**
     * 姓名TextView
     */
    private TextView userNameTextView;

    /**
     * 签名TextView
     */
    private TextView userSignTextView;

    /**
     * 头像ImageView
     */
    private SmartImageView userIcImageView;

    /**
     * 食物 ImageView
     */

    private SmartImageView foodImageView;

    /**
     * 内容简介TextView
     */
    private TextView contentTextView;

    private final int DATA = 2;

    /**
     * 数据List
     */
    private List<DataBean> dataBeans;

    /**
     * 用户Bean
     */
    private DataBean userDataBean;

    /**
     * 食物Bean
     */
    private DataBean foodDataBean;

    /**
     * 简介内容Bean
     */
    private DataBean contentBean;

    /**
     * 店铺内容Bean
     */
    private DataBean shopDataBean;

    private Handler handler;

    /**
     * 点击监听接口声明
     */
    private View.OnClickListener clickListener;
    private Object data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delicious_food_);

        //初始化视图
        initView();
        //获取数据
        getData();
    }

    @Override
    public void onClick(View v) {

    }

    private void initView() {
        shopImageView = (SmartImageView) findViewById(R.id.recommendActivityFoodImageView);
        backTextView = (TextView) findViewById(R.id.recommendActivity);
        userNameTextView = (TextView) findViewById(R.id.recommendActivityUserName);
        userSignTextView = (TextView) findViewById(R.id.recommendActivityUserSign);
        foodImageView = (SmartImageView) findViewById(R.id.recommendActivityFoodImageView);
        contentTextView = (TextView) findViewById(R.id.recommendActivityContent);
        userIcImageView = (SmartImageView) findViewById(R.id.recommendActivityUserIcImageView);

        //透明动画 从完全不透明到完全透明的一个动画效果
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f,1f);

        alphaAnimation.setDuration(2500);//设置动画持续时间4S
        alphaAnimation.setFillAfter(false);//设置动画结束之后的效果
        alphaAnimation.setInterpolator(this,android.R.interpolator.accelerate_decelerate);

        //播放动画
        shopImageView.startAnimation(alphaAnimation);//播放动画

        //实例化点击监听类
        clickListener = new InformationOnClickListener();

        //设置返回按钮点击监听
        backTextView.setOnClickListener(clickListener);

        //实例化Handler,通过自定义Callback类
        handler = new Handler(new InformationCallback());

    }

    /**
     * 获取数据
     */

    private void getData() {
    //实例化InformationRunnabale类
    InformationRunnabale informationRunnabale = new InformationRunnabale();
    //传入InformationRunnabale实例化Thread
        Thread thread = new Thread(informationRunnabale);
        //开启线程
        thread.start();

    }


    private class InformationCallback implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            //根据消息不同msg值作出不同处理


            return false;
        }
    }

    private class InformationRunnabale implements Runnable{
        /**
         * 记录Bean类型
         */
        String type = null;

        @Override
        public void run(){
            //从网络工具类获取dataBeans
            dataBeans = NetUtil.getDataBeanListDataForRecommendInformation
                    (getIntent().getStringExtra(RecommendFragment.INFORMATION_KEY));
            //如果获取数据不为空
            Log.d("TAGG", dataBeans.size() + "");
            if (null != dataBeans){
                //遍历dataBeans
                for (DataBean dataBean : dataBeans){
                    type = dataBean.getType();
                    //如果是UserCard类型
                    if (DataBean.TYPE_USER_CARD.equals(type)){
                        //获取到UserCard类型的Bean
                        userDataBean = dataBean;
                        Log.d("TAGG",userDataBean.getTitle());
                    }else if (DataBean.TYPE_IMAGE_CARD.equals(type)){
                        //获取ImageCard类型的Bean
                        foodDataBean = dataBean;
                    }else if (DataBean.TYPE_CONTENT_CARD.equals(type)){
                        //获取ContentCard类型的Bean
                        contentBean = dataBean;
                    }else if (DataBean.TYPE_PIC_CARD.equals(type)){
                        //获取PicCard类型的店铺Bean
                        shopDataBean = dataBean;
                    }
                }
                //通过handler获取消息
                Message msg = handler.obtainMessage();
                //设置消息what值
                msg.what = DATA;
                //发送消息
                handler.sendMessage(msg);
            } else {
                Log.i(LOG_TAG, "data is null");
            }
        }
    }

    /**
     * 点击事件监听类
     */
    private class InformationOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v){
            switch (v.getId()){
                case  R.id.recommendActivity:
                    //销毁当前Activity
                    finish();
                    break;
            }

        }

    }
}
